<template>
  <xy-page>
    <div class="liveSwitch">
      <h3>功能开关</h3>
      <el-form :model="form" status-icon ref="form" class="mgt40">
        <el-form-item label="直播web观看链接:" :label-width="labwidth">
          <el-radio-group v-model="form.isClosePreview" class="mgl20" @change="switchVal('isClosePreview')">
            <el-radio label="1">开</el-radio>
            <el-radio label="0">关</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="观看页在线列表:" :label-width="labwidth">
          <el-radio-group v-model="form.closeChaterList" class="mgl20" @change="switchVal('closeChaterList')">
            <el-radio label="1">开</el-radio>
            <el-radio label="0">关</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提问:" :label-width="labwidth">
          <el-radio-group v-model="form.consultingMenu" class="mgl20" @change="switchVal('consultingMenu')">
            <el-radio label="1">开</el-radio>
            <el-radio label="0">关</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="聊天室:" :label-width="labwidth">
          <el-radio-group v-model="form.chat" class="mgl20" @change="switchVal('chat')">
            <el-radio label="1">开</el-radio>
            <el-radio label="0">关</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="观众发送图片:" :label-width="labwidth">
          <el-radio-group v-model="form.viewerSendImgEnabled" class="mgl20" @change="switchVal('viewerSendImgEnabled')">
            <el-radio label="1">开</el-radio>
            <el-radio label="0">关</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="弹幕功能:" :label-width="labwidth">
          <el-radio-group v-model="form.closeDanmu" class="mgl20" @change="switchVal('closeDanmu')">
            <el-radio label="1">开</el-radio>
            <el-radio label="0">关</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="欢迎语功能:" :label-width="labwidth">
          <el-radio-group v-model="form.welcome" class="mgl20" @change="switchVal('welcome')">
            <el-radio label="1">开</el-radio>
            <el-radio label="0">关</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
  </xy-page>
</template>

<script>
export default {
  data() {
    return {
      labwidth: '150px',
      form: {
        isClosePreview: '',
        closeChaterList: '',
        consultingMenu: '',
        chat: '',
        viewerSendImgEnabled: '',
        closeDanmu: '',
        welcome: '',
      },
      channelids:'',
      appId:''
    }
  },
  methods: {
    getSwitch(){ //获取功能开关列表
      this.$postStream('/admin/instance/ccb/polyv/channel/switch/get.do',{
        data:{
          appId: this.appId,
          channelId: this.channelids,
        },
        success:res=>{
          if(res.success){
            res.object.list.forEach(element => {
              if(element.type == 'isClosePreview' || element.type == 'closeChaterList' || element.type == 'closeDanmu'){
                element.enabled = (1 - this.switchVals(element.enabled)) + ''
              }else{
                element.enabled = this.switchVals(element.enabled) + ''
              }
              this.form[element.type] = element.enabled
            })
            this.$forceUpdate()
          }else{
            this.$message.error('获取功能开关失败!')
          }
          
        }
      })
    },
    selectType(item){
      if(item==="isClosePreview"||item==="closeChaterList"||item==="closeDanmu"){
        return this.form[item]==='0'? 'Y':'N'
      } 
      else{
        return this.form[item]==='0'? 'N':'Y'
      }
    },
    switchVal(type) {//修改功能开关
      let item = type
      this.$postStream('/admin/instance/ccb/polyv/channel/switch/update.do',{
        data:{
          appId: this.appId,
          channelId: this.channelids,
          // enabled: this.selectType(this.form[item]),
          enabled:this.selectType(item),
          type: item
        },
        success:res=>{
          if(res.success){
            this.$message.success('修改成功!')
            this.getSwitch()
          }else{
            res.code == 50001?this.$message.error(res.message):this.$message.error('修改失败!')
            if(this.form[item] == '0'){
              this.form[item] = '1'
            }else{
              this.form[item] = '0'
            }
          }
        }
      })
    },
  },
  mounted() {
    this.channelids = sessionStorage.getItem('channelId')
    this.appId = sessionStorage.getItem('appId') || ''
    this.getSwitch()
  }
};
</script>

<style lang="" scoped>
</style>
